package com.man;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.std.CurrentUser;
import com.std.DB;
import com.std.User;
import com.sun.org.apache.regexp.internal.REUtil;

public class UserCtrl {
	private DB db;
	public UserCtrl() {
		db=new DB();
	}
	public void close() {
		db.close();
	}
	public CurrentUser insertItem(String username,String pwd,String privilege,
			String name,String sex,String address,String phone,String postcode,
			String email) {
		String sql="insert into user_t values(?,?,?,?,?,?,?,?,?,current_date)";
		Object []params={username,pwd,privilege,name,sex,address,phone,postcode,email};
		db.execute(sql, params);
		CurrentUser currentUser=new CurrentUser(username, privilege);
		return currentUser;
	}
	public List<User> getItems() {
		String sql="select * from user_t";
		db.execute(sql, null);
		List<User> userList=new ArrayList<User>();
		ResultSet rs=db.getResultset();
		try {
			while(rs.next()){
				User user=new User();
				user.setUsername(rs.getString("username"));
				user.setSex(rs.getString("sex"));
				user.setAddress(rs.getString("address"));
				user.setEmail(rs.getString("email"));
				user.setName(rs.getString("name"));
				user.setPhone(rs.getString("phone"));
				user.setPostcode(rs.getString("postcode"));
				user.setPrivilege(rs.getString("privilege"));
				user.setPwd(rs.getString("pwd"));
				user.setRegisterTime(rs.getDate("register_time"));
				userList.add(user);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return userList;
	}
	public User getItem(String username) {
		String sql="select * from user_t where username=?";
		Object []params={username};
		db.execute(sql, params);
		ResultSet rs=db.getResultset();
		try {
			if(rs.next()){
				User user=new User();
				user.setUsername(rs.getString("username"));
				user.setSex(rs.getString("sex"));
				user.setAddress(rs.getString("address"));
				user.setEmail(rs.getString("email"));
				user.setName(rs.getString("name"));
				user.setPhone(rs.getString("phone"));
				user.setPostcode(rs.getString("postcode"));
				user.setPrivilege(rs.getString("privilege"));
				user.setPwd(rs.getString("pwd"));
				user.setRegisterTime(rs.getDate("register_time"));
				return user;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	public void deleteItem(String username) {
		String sql="delete from user_t where username=?";
		Object []params={username};
		db.execute(sql, params);
		
	}
	public void updateItem(String username,String pwd,String privilege,
			String name,String sex,String address,String phone,String postcode,
			String email) {
		String sql="update user_t set pwd=?,privilege=?,name=?,sex=?,address=?,phone=?,postcode=?,email=? where username=?";
		Object []params={pwd,privilege,name,sex,address,phone,postcode,email,username};
		db.execute(sql, params);
		
	}
	public CurrentUser validate(String username,String pwd) {
		String sql="select * from user_t where username=? and pwd=?";
		Object []params={username,pwd};
		db.execute(sql, params);
		ResultSet rs=db.getResultset();
		try {
			if(rs.next()){
				CurrentUser currentUser=new CurrentUser();
				currentUser.setPrivilege(rs.getString("privilege"));
				currentUser.setUsername(rs.getString("username"));
				return currentUser;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
}
